import datetime
import uuid
import os

def save_file(base_dir, file):
    now = datetime.datetime.now()
    date_dir = str(now.year) + str(now.month) + str(now.day)
    target_dir = base_dir + date_dir + "/"
    if not os.path.exists(target_dir):
        os.mkdir(target_dir)
    
    #file_name = file['filename']
    file_name = "a.jpg"
    
    suffix = get_file_suffix(file_name)
    filename = uuid.uuid5(uuid.NAMESPACE_DNS, file_name + str(now)).hex + suffix
    fd = open('%s/%s' % (target_dir, filename), 'wb')
    for chunk in file.chunks():
        fd.write(chunk)
    #fd.write(file['content'])
    fd.close()
    return date_dir + "/" + filename

def get_file_suffix(file_name):
    index = file_name.rfind(".")
    if index != - 1:
        return file_name[index:]
    return ""